#include <stdio.h>
#include <stdbool.h>

int* sortArrayByParity(int* nums, int numsSize, int* returnSize) {
    *returnSize = numsSize;
    int begin = 0;
    int end = numsSize - 1;
    while(end - begin > 0) {
        int bg = begin;
        while(bg < end){
            if(nums[bg] % 2 == 1)
                break;
            bg++;
        }
        int ed = end;
        while(ed > begin){
            if(nums[ed] % 2 == 0)
                break;
            ed--;
        }
        if(bg < ed) {
            int tmp = nums[ed];
            nums[ed] = nums[bg];
            nums[bg] = tmp;
        }
        begin = bg + 1;
        end = ed - 1;
    }
    return nums;
}

int main() {
    int nums[] = {2,4,3,1};
    int numsSize = sizeof(nums) / sizeof(int);
    int size;
    int * rs = sortArrayByParity(nums, numsSize, &size);

    for(int i = 0; i < size; i++)
        printf(" %d ", rs[i]);
    return 0;
}